package com.yedean.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yedean.mall.dto.PmsProductParam;
import com.yedean.mall.entity.*;
import com.yedean.mall.mapper.PmsMemberPriceMapper;
import com.yedean.mall.mapper.PmsProductMapper;
import com.yedean.mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author yedean
 * @since 2022-08-09
 */
@Service
public class PmsProductServiceImpl extends ServiceImpl<PmsProductMapper, PmsProduct> implements IPmsProductService {
    @Resource
    private PmsProductMapper pmsProductMapper;
    @Resource
    private IPmsMemberPriceService memberPriceService;
    @Resource
    private IPmsProductLadderService productLadderService;
    @Resource
    private IPmsProductFullReductionService fullReductionService;
    @Resource
    private IPmsSkuStockService skuStockService;
    @Resource
    private IPmsProductAttributeValueService attributeValueService;
    @Override
    public void create(PmsProductParam productParam) {
        PmsProduct product = new PmsProduct();
        BeanUtil.copyProperties(productParam,product);
        baseMapper.insert(product);
        //根据促销类型设置价格：会员价格、阶梯价格、满减价格
        Long productId = product.getId();
        //添加会员价格
        List<PmsMemberPrice> priceList = productParam.getMemberPriceList().stream()
                .peek(pmsMemberPrice -> pmsMemberPrice.setProductId(productId))
                .collect(Collectors.toList());
        memberPriceService.saveOrUpdateBatch(priceList);
        //添加阶梯价格
        List<PmsProductLadder> ladderList = productParam.getProductLadderList().stream()
                .peek(ladder -> ladder.setProductId(productId))
                .collect(Collectors.toList());
        productLadderService.saveOrUpdateBatch(ladderList);
        //添加满减价格
        List<PmsProductFullReduction> fuuReduceList = productParam.getProductFullReductionList().stream()
                .peek(fullReduce -> fullReduce.setProductId(productId))
                .collect(Collectors.toList());
        fullReductionService.saveOrUpdateBatch(fuuReduceList);
        //处理sku的编码
        handleSkuStockCode(productParam.getSkuStockList(),productId);
        //添加sku库存信息
        skuStockService.saveBatch(productParam.getSkuStockList());
        //添加商品参数,添加自定义商品规格
        List<PmsProductAttributeValue> valueList = productParam.getProductAttributeValueList().stream()
                .peek(productAttributeValue -> productAttributeValue.setProductId(productId))
                .collect(Collectors.toList());
        attributeValueService.saveBatch(valueList);
    }


    private void handleSkuStockCode(List<PmsSkuStock> skuStockList, Long productId) {
        if(CollectionUtils.isEmpty(skuStockList)){
            return;
        }
        for(int i=0;i<skuStockList.size();i++){
            PmsSkuStock skuStock = skuStockList.get(i);
            if(StringUtils.isEmpty(skuStock.getSkuCode())){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                StringBuilder sb = new StringBuilder();
                //日期
                sb.append(sdf.format(new Date()));
                //四位商品id
                sb.append(String.format("%04d", productId));
                //三位索引id
                sb.append(String.format("%03d", i+1));
                skuStock.setSkuCode(sb.toString());
            }
        }
    }
}
